package com.nbpt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.nbpt.db.AccountDbHelper;

public class RegisterActivity1 extends AppCompatActivity {
    EditText edtUsername;
    EditText edtPasspword;
    EditText editText3;
    EditText editText4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register1);
        edtUsername = findViewById(R.id.edtUsername);

        edtUsername.setTextColor(getResources().getColor(R.color.colorAccent));
        edtPasspword = findViewById(R.id.edtPassword);
        editText3 = findViewById(R.id.editText3);
        editText4 = findViewById(R.id.editText4);


    }

    public void Register(View view) {
        String username = edtUsername.getText().toString();
        String password = edtPasspword.getText().toString();
        String Text3 = editText3.getText().toString();
        String Text4 = editText4.getText().toString();
        Log.d("RegisterActivity", "点击了注册按钮");
        Toast.makeText(this, "账号:" + username + "密码:" + password + "密码2" + Text3 + "手机号码：" + Text4, Toast.LENGTH_SHORT).show();
        if (!username.isEmpty() && !password.equals("") && !Text3.isEmpty() && !Text4.isEmpty()) {
            if (password.equals(Text3)) {
                AccountDbHelper dbHelper = new AccountDbHelper(RegisterActivity1.this);
                long result = dbHelper.insertNewAccount(username, password, 1, Text4);
                if (result > 0) {
                    Toast.makeText(RegisterActivity1.this, "注册成功", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.putExtra("extra0", username);
                    setResult(1, intent);
                    intent.putExtra("extra2", password);
                    setResult(1, intent);
                    finish();
                } else if (result == -1) {
                    Toast.makeText(RegisterActivity1.this, "账号已存在", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(RegisterActivity1.this, "账号注册信息输入不完整", Toast.LENGTH_LONG).show();
                }


            }
        }
    }
}